package com.zhtools.utils;

import org.apache.commons.lang.StringUtils;

import java.util.Calendar;
import java.util.Date;

/**
 * @author zhanghao
 * create by zhanghao at 2018/05/08
 */
public final class AgeUtils {
    public static Integer getAge(Date birthDay) {
        if (birthDay == null) {
            return 0;
        }
        int currentYear = Calendar.getInstance().get(Calendar.YEAR);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(birthDay);
        int birthYear = calendar.get(Calendar.YEAR);
        int age = currentYear - birthYear;
        if (age <= 0) {
            return 0;
        }
        return age;
    }

    public static Integer getAge(String birthYear) {
        if (StringUtils.isEmpty(birthYear)) {
            return 0;
        }
        int intBirthYear = Integer.parseInt(birthYear);
        int currentYear = Calendar.getInstance().get(Calendar.YEAR);
        int age = currentYear - intBirthYear;
        if (age <= 0) {
            return 0;
        }
        return age;
    }
}
